import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:my_app/common/common.dart';
import 'package:my_app/service/encrypt/encrypt.dart';

class HttpRequest {
  // 请求头
  static const httpHeaders = {
    'Accept': 'application/json',
    'Content-Type': 'application/json',
  };
  // get 请求
  static get() async {
    late Response response;
    var dio = Dio();

    response = await dio.get('http://127.0.0.1:3002/login');
    print(response.data.toString());
  }

  // post list请求
  static postList(String url, String param, String mapsuid,
      [header = httpHeaders]) async {
    var sql = Encrypt.aesEncrypt(param);
    var sign = Encrypt.md5Encryp(sql);
    var data = {
      "sql": sql,
      "sign": sign,
      "mapsuid": mapsuid,
      "isencryption": false
    };
    late Response response;
    var dio = Dio();
    // 添加请求头
    dio.options.headers = header;
    // 最大连接时间
    dio.options.connectTimeout = 10000;
    // 最大等待服务器返回时间
    dio.options.receiveTimeout = 10000;
    // dio.interceptors.add(InterceptorsWrapper(
    //   onRequest: (options, handler) {
    //     print("---------------------onRequest");
    //     return handler.next(options);
    //   },
    //   onResponse: (e, handler) {
    //     print("---------------------onResponse");
    //     print(e.toString());
    //     // print(handler.toString());
    //     return handler.next(e);
    //   },
    //   onError: (DioError err, handler) {
    //     print("---------------------onError");
    //     print('ERROR[${err.response!.statusCode}]');
    //     Common.showTost(err.response!.statusCode.toString());
    //   },
    // ));
    // dio.interceptors.add(LogInterceptor(responseBody: true));
    try {
      //404
      response = await dio.post(url, data: data);
      // 成功流程
      if (response.data["statusString"] == "2000") {
        print("_________http-response.data_________________");
        // print(response.data);
        // print(response.requestOptions);
        // print(response.statusCode);
        // print("_________http_________________end");
        return response.data["dataObject"];
      } else {
        // 失败流程
        Common.showTost(response.data.toString(), Colors.deepOrange);
        print("___________postList失败__________________");
        print(response.data["statusString"]);
      }
    } on DioError catch (e) {
      print("DioError");
      // The request was made and the server responded with a status code
      // that falls out of the range of 2xx and is also not 304.
      if (e.response != null) {
        print("if_DioError");

        // print(e.response!.data);
      } else {
        print("else_DioError");

        // Something happened in setting up or sending the request that triggered an Error
        print(e.requestOptions);
        print(e.message);
      }
    }
  }

  // post string请求
  static postString(String url, String param, String mapsuid,
      [header = httpHeaders]) async {
    print(param);
    var sql = Encrypt.aesEncrypt(param);
    var sign = Encrypt.md5Encryp(sql);
    var data = {
      "sql": sql,
      "sign": sign,
      "mapsuid": mapsuid,
      "isencryption": false
    };
    late Response response;
    var dio = Dio();
    // 添加请求头
    dio.options.headers = header;
    // 最大连接时间
    dio.options.connectTimeout = 5000;
    // 最大等待服务器返回时间
    dio.options.receiveTimeout = 5000;
    response = await dio.post(url, data: data);
    // 成功流程
    if (response.data["statusString"] == "2000") {
      print("_________http-response.data.msgString_________________");
      print(response.data["msgString"]);
      return response.data["msgString"];
    } else {
      // 失败流程
      Common.showTost(response.data.toString(), Colors.deepOrange);
      print(response.data["statusString"]);
    }
  }
}
